import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ButtonNavigation extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ButtonNavigationState();
  }
}

class _ButtonNavigationState extends State<ButtonNavigation> {
  var _currentIndex = 0;
  PageController _pageController;
  List<Widget> _page = [
    Center(
      child: Text('第一个页面'),
    ),
    Center(
      child: Text('第二个页面'),
    ),
    Center(
      child: Text('第三个页面'),
    ),
    Center(
      child: Text('第四个页面'),
    )
  ];

  currentPage() {
    switch (_currentIndex) {
      case 0:
        return Center(
          child: Text('第一个页面'),
        );
        break;
      case 1:
        return Center(
          child: Text('第二个页面'),
        );
        break;
      case 2:
        return Center(
          child: Text('第三个页面'),
        );
        break;
      case 3:
        return Center(
          child: Text('第四个页面'),
        );
        break;
    }
  }

  @override
  void initState() {
    super.initState();
    //初始化PageController
    _pageController = PageController(initialPage: _currentIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('ButtonNavigation'),
        ),
        bottomNavigationBar: BottomAppBar(
          color: Colors.white,
          shape: CircularNotchedRectangle(),
          child: Row(
            children: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.home,
                  color: _currentIndex == 0 ? Colors.blue : Colors.black,
                ),
                onPressed: () {
                  setState(() {
                    _currentIndex = 0;
                    this._pageController.jumpToPage(this._currentIndex);
                  });
                },
              ),
              IconButton(
                icon: Icon(Icons.business,
                    color: _currentIndex == 1 ? Colors.blue : Colors.black),
                onPressed: () {
                  setState(() {
                    _currentIndex = 1;
                    this._pageController.jumpToPage(this._currentIndex);
                  });
                },
              ),
              SizedBox(), //中间位置空出
              IconButton(
                icon: Icon(Icons.add_shopping_cart,
                    color: _currentIndex == 2 ? Colors.blue : Colors.black),
                onPressed: () {
                  setState(() {
                    _currentIndex = 2;
                    this._pageController.jumpToPage(this._currentIndex);
                  });
                },
              ),
              IconButton(
                icon: Icon(Icons.face,
                    color: _currentIndex == 3 ? Colors.blue : Colors.black),
                onPressed: () {
                  setState(() {
                    _currentIndex = 3;
                    this._pageController.jumpToPage(this._currentIndex);
                  });
                },
              ),
            ],
            mainAxisAlignment: MainAxisAlignment.spaceAround, //均分底部导航栏横向空间
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: FloatingActionButton(
          onPressed: () {},
          tooltip: 'Increment',
          child: new Icon(Icons.add),
        ),
        body: PageView(
          controller: _pageController,
          children: _page,
          onPageChanged: (int index){
            setState(() {
              _currentIndex = index;
            });
          },
        ));
  }
}
